﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Client.Helper;
using Zwms.Data.CommonObject;
using System.IO;
using NPOI.HSSF.UserModel;
using System.Data.Common;
using System.Data.SQLite;

namespace Zwms.Client.ManageWms
{
    public partial class CountBillCreatWnd : UserControl
    {
        private BilCountBill _currentCountBill;
        private BilCountBillDetail _itemBillDetail;
        private IList<BilCountBillDetail> _detailList;
        private IList<BilCountBillDetail> _subDetailList;
        private IList<BilCountBillDetail> _deleteList;
        private PagerIndex _pagerIndex;
        private bool _isNew;

        public CountBillCreatWnd()
        {
            InitializeComponent();
            this.InitBtnImage();
            _pagerIndex = new PagerIndex(1, int.MaxValue);
            this._currentCountBill = new BilCountBill();
            this._detailList = new List<BilCountBillDetail>();
            this._subDetailList = new List<BilCountBillDetail>();
            this._deleteList = new List<BilCountBillDetail>();
            this._isNew = false;
            this.SetControlEditState(false);
        }

        private void InitBtnImage()
        {
            this.btnNew.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnEdit.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnAddDtl.Image = global::Zwms.Client.Properties.Resources.addall;
            this.btnDeleteDtl.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnSave.Image = global::Zwms.Client.Properties.Resources.save;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;

            this.btnExport.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnSubmit.Image = global::Zwms.Client.Properties.Resources.ok;
            this.BtnDBFile.Image = global::Zwms.Client.Properties.Resources.refresh;
            this.btnPrintRpt.Image = global::Zwms.Client.Properties.Resources.print;
            this.btnCancelBill.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void SetRecordToUI(BilCountBill record)
        {
            this.tbCountBillCode.Text = record.Id;
            this.tbsWarehouse.SetTextBoxContent(record.WarehouseName);
            this.tbsWarehouse.Tag = record.WarehouseCode;
            this.dpuPlanCountDate.SetDate(CommonHelper.GetDatetimeFromString(record.PlanCountDate));
            this.dpuCountDate.SetDate(CommonHelper.GetDatetimeFromString(record.CountDate));
            this.cbBillStatus.SelectedIndex = cbBillStatus.Items.IndexOf(CommonDefine.DictCountBillStatus[record.BillStatus]);
            this.tbCreateUser.Text = record.CreateUserName;
            this.tbEditUser.Text = record.EditUserName;
            this.dpuCreateDate.SetDate(CommonHelper.GetDatetimeFromString(record.CreateDate));
            this.dpuEditDate.SetDate(CommonHelper.GetDatetimeFromString(record.EditDate));
        }

        private void ClearUI()
        {
            this.tbCountBillCode.Text = string.Empty;
            this.tbsWarehouse.SetTextBoxContent(string.Empty);
            this.tbsWarehouse.Tag = null;
            this.dpuPlanCountDate.SetVoidDate();
            this.dpuCountDate.SetVoidDate();
            this.cbBillStatus.SelectedIndex = -1;
            this.tbCreateUser.Text = string.Empty;
            this.tbEditUser.Text = string.Empty;
            this.dpuCreateDate.SetVoidDate();
            this.dpuEditDate.SetVoidDate();
            this.bilCountBillDetailBindingSource.DataSource = null;
        }

        private void SetMenuEditState(bool b)
        {
            this.btnNew.Enabled = !b;
            this.btnEdit.Enabled = !b;
            this.btnQuery.Enabled = !b;
            this.btnExport.Enabled = !b;
            this.btnSubmit.Enabled = !b;

            this.btnAddDtl.Enabled = b;
            this.btnDeleteDtl.Enabled = b;
            this.btnSave.Enabled = b;
            this.btnCancel.Enabled = b;
        }

        private void SetControlEditState(bool b)
        {
            this.tbsWarehouse.SetEditState(b);
            this.dpuPlanCountDate.SetEditState(b);
        }

        private void LoadSubList(int startIndex, int endIndex)
        {
            this._subDetailList.Clear();
            if (startIndex >= this._detailList.Count)
                return;
            if (endIndex >= this._detailList.Count)
                endIndex = this._detailList.Count - 1;
            BilCountBillDetail item = new BilCountBillDetail();
            for (int i = startIndex; i <= endIndex; i++)
            {
                item = this._detailList.ElementAt(i);
                this._subDetailList.Add(item);
            }

        }

        private BilCountBill GetUCBill()
        {
            if (string.IsNullOrWhiteSpace(this.tbCountBillCode.Text.Trim()))
            {
                return null;
            }
            BilCountBill ucRecord = new BilCountBill();
            ucRecord.Id = this.tbCountBillCode.Text.Trim();
            if (this._isNew)
            {
                ucRecord.WarehouseCode = tbsWarehouse.Tag.ToString();
                ucRecord.WarehouseName = tbsWarehouse.GetTextBoxContent();
                ucRecord.PlanCountDate = dpuPlanCountDate.SelectDateTime;
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                ucRecord.CreateUserCode = MainForm.CurrentUser.Id;
                ucRecord.CreateUserName = MainForm.CurrentUser.UserName;
                ucRecord.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                ucRecord.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                ucRecord.BillStatus = "0";
                ucRecord.IsDeactived = "0";
            }
            else
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                ucRecord = client.QueryCountBill(ucRecord);
                ucRecord.WarehouseCode = tbsWarehouse.Tag.ToString();
                ucRecord.WarehouseName = tbsWarehouse.GetTextBoxContent();
                ucRecord.PlanCountDate = dpuPlanCountDate.SelectDateTime;
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                ucRecord.EditUserCode = MainForm.CurrentUser.Id;
                ucRecord.EditUserName = MainForm.CurrentUser.UserName;
                ucRecord.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                ucRecord.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
            }
            return ucRecord;

        }

        private BilCountBillDetail recordConvert(StoGoodsStock stoRecord)
        {
            if (stoRecord == null)
            {
                return null;
            }
            BilCountBillDetail cbRecord = new BilCountBillDetail();
            cbRecord.CountBillCode = this.tbCountBillCode.Text.Trim();
            cbRecord.StockCode = stoRecord.Id;
            cbRecord.IBCode = stoRecord.IBCode;
            cbRecord.IBDate = stoRecord.IBDate;
            cbRecord.WarehouseCode = stoRecord.WarehouseCode;
            cbRecord.WarehouseName = stoRecord.WarehouseName;
            cbRecord.AreaCode = stoRecord.AreaCode;
            cbRecord.AreaName = stoRecord.AreaName;
            cbRecord.ShelfCode = stoRecord.ShelfCode;
            cbRecord.ShelfName = stoRecord.ShelfName;
            cbRecord.LocationCode = stoRecord.LocationCode;
            cbRecord.LocationName = stoRecord.LocationName;
            cbRecord.GoodsCode = stoRecord.GoodsCode;
            cbRecord.GoodsName = stoRecord.GoodsName;
            cbRecord.Spec = stoRecord.GoodsSpec;
            cbRecord.PkgQty = stoRecord.PackageQty;
            cbRecord.PieceQty = stoRecord.PieceQty;
            cbRecord.GoodsQty = stoRecord.GoodsTotalQty;
            return cbRecord;
        }

        private void LoadCountBillDetail(string sCode)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            BilCountBillDetail queryRecord = new BilCountBillDetail();
            queryRecord.CountBillCode = sCode;
            _pagerIndex.CurrentPageIndex = 1;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                this._detailList = client.QueryCountBillDetailList(queryRecord, ref _pagerIndex);
                this._detailList = this._detailList.ToList();
                this.LoadSubList(0, CommonDefine.iPageSize);
                this.bilCountBillDetailBindingSource.DataSource = null;
                this.bilCountBillDetailBindingSource.DataSource = this._subDetailList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }


        #region 控件函数
        private void btnQuery_Click(object sender, EventArgs e)
        {
            CountBillSelectForm window = new CountBillSelectForm();
            if (window.ShowDialog() == DialogResult.OK)
            {
                this._currentCountBill = window.SelectedRecord;
                this.SetRecordToUI(this._currentCountBill);
                this.LoadCountBillDetail(this._currentCountBill.Id);

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            GoodsStockSelForm window = new GoodsStockSelForm(false);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (StoGoodsStock item in window.StockList)
                {
                    this._itemBillDetail = this.recordConvert(item);
                    this._itemBillDetail.EditFlag = "new";
                    this._detailList.Add(this._itemBillDetail);
                }
                this.LoadSubList(0, CommonDefine.iPageSize - 1);
                this.bilCountBillDetailBindingSource.DataSource = null;
                this.bilCountBillDetailBindingSource.DataSource = this._subDetailList;
                dataPager.RecordCount = this._deleteList.Count;
                _pagerIndex.CurrentPageIndex = 1;
                dataPager.PageCurrent = 1;
            }

        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
                return;
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this._itemBillDetail = this._subDetailList[rowIndex];
                this._deleteList.Add(this._itemBillDetail);
                this._detailList.Remove(this._itemBillDetail);
                _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
                int startIndex = CommonDefine.iPageSize * (dataPager.PageCurrent - 1);
                int endIndex = startIndex + CommonDefine.iPageSize - 1;
                this.LoadSubList(startIndex, endIndex);
                this.bilCountBillDetailBindingSource.DataSource = null;
                this.bilCountBillDetailBindingSource.DataSource = this._subDetailList;
                this.dataPager.RecordCount = this._detailList.Count;
                return;
            }
        }



        private void btnNew_Click(object sender, EventArgs e)
        {
            NewCountBillForm window = new NewCountBillForm();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.SetRecordToUI(window.CurrentRecord);
                this.SetMenuEditState(true);
                this.SetControlEditState(true);
                this.bilCountBillDetailBindingSource.DataSource = null;
                this._isNew = true;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.tbCountBillCode.Text.Trim() == "")
            {
                MessageBox.Show("请选择盘点单！");
                return;
            }
            if (this.cbBillStatus.SelectedIndex != 0)
            {
                MessageBox.Show("当前单据状态不可编辑！");
                return;
            }
            this.SetMenuEditState(true);
            this.SetControlEditState(true);
            this._isNew = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            BilCountBill ucRecord = this.GetUCBill();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            try
            {
                this.Cursor = Cursors.WaitCursor;
                //添加盘点单
                client.AddCountBill(ucRecord);
                //添加盘点明细单记录
                foreach (BilCountBillDetail item in this._detailList)
                {
                    if (item.EditFlag.Trim() == "new")
                        client.AddCountBillDetail(item);
                }
                //删除盘点明细单记录
                foreach (BilCountBillDetail item in this._deleteList)
                {
                    client.DeleteCountBillDetail(item);
                }
                this.Cursor = Cursors.Default;
                MessageBox.Show("保存成功！");
                InfoSys.SysLogWriter.WriteOptLog("生成|编辑盘点单", "CountBillSave", "保存");
                this.SetMenuEditState(false);
                this.SetControlEditState(false);
                this.ClearUI();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定不对当前修改进行保存？", "确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.SetMenuEditState(false);
                this.SetControlEditState(false);
                this.ClearUI();
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.tbCountBillCode.Text.Trim() == "")
            {
                MessageBox.Show("没有选择任何盘点单，请重新选择！");
                return;
            }

            if (MessageBox.Show("确认制单完毕，提交至下一状态", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                BilCountBill ucRecord = new BilCountBill();
                ucRecord.Id = this.tbCountBillCode.Text.Trim();
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                ucRecord = client.QueryCountBill(ucRecord);
                if (ucRecord.BillStatus != "0")
                {
                    MessageBox.Show("单据状态为制单才可进行此操作！");
                    return;
                }
                ucRecord.BillStatus = "1";
                if (client.UpdateCountBill(ucRecord))
                {
                    this.cbBillStatus.SelectedIndex = 1;
                    MessageBox.Show("提交成功！");
                    InfoSys.SysLogWriter.WriteOptLog("确认盘点单", "CountBillSubmit", "提交");
                }
                else
                {
                    MessageBox.Show("提交失败！");
                }
            }

        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            int startIndex = _pagerIndex.CurrentPageIndex * CommonDefine.iPageSize;
            int endIndex = startIndex + CommonDefine.iPageSize - 1;
            this.LoadSubList(startIndex, endIndex);
            this.bilCountBillDetailBindingSource.DataSource = this._subDetailList;
        }

        #endregion

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbCountBillCode.Text))
            {
                MessageBox.Show("请选择盘点单！");
                return;
            }
            SaveFileDialog fileDlg = new SaveFileDialog();
            fileDlg.Filter = "*.xls|*.xls";
            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                string sTemplate = System.Windows.Forms.Application.StartupPath + @"\Template\盘点单模板.xls";
                if (!File.Exists(sTemplate))
                {
                    MessageBox.Show("模板文件不存在！");
                    return;
                }
                string sOutputDir = Path.GetDirectoryName(fileDlg.FileName);
                //打开文件
                FileStream fileTemplate = new FileStream(sTemplate, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileTemplate);
                //获取工作簿
                HSSFSheet sheet = hssfworkbook.GetSheet("Sheet1") as HSSFSheet;

                //填写盘点单基本信息
                sheet.GetRow(2).GetCell(2).SetCellValue(this._currentCountBill.Id);
                sheet.GetRow(2).GetCell(6).SetCellValue(this._currentCountBill.WarehouseName);
                sheet.GetRow(3).GetCell(2).SetCellValue(this._currentCountBill.PlanCountDate);
                sheet.GetRow(3).GetCell(6).SetCellValue(this._currentCountBill.CountDate);
                sheet.GetRow(4).GetCell(2).SetCellValue(this._currentCountBill.CountOperatorName);
                sheet.GetRow(5).GetCell(2).SetCellValue(this._currentCountBill.CreateUserName);
                sheet.GetRow(5).GetCell(6).SetCellValue(this._currentCountBill.CreateDate);

                //填写明细
                int iRow = 8;
                foreach (BilCountBillDetail record in this._detailList)
                {
                    sheet.CreateRow(iRow);
                    sheet.GetRow(iRow).CreateCell(0);
                    sheet.GetRow(iRow).CreateCell(1);
                    sheet.GetRow(iRow).CreateCell(2);
                    sheet.GetRow(iRow).CreateCell(3);
                    sheet.GetRow(iRow).CreateCell(4);
                    sheet.GetRow(iRow).CreateCell(5);
                    sheet.GetRow(iRow).CreateCell(6);
                    sheet.GetRow(iRow).CreateCell(7);
                    sheet.GetRow(iRow).CreateCell(8);
                    sheet.GetRow(iRow).CreateCell(9);
                    sheet.GetRow(iRow).GetCell(0).SetCellValue(record.GoodsCode);
                    sheet.GetRow(iRow).GetCell(1).SetCellValue(record.GoodsName);
                    sheet.GetRow(iRow).GetCell(2).SetCellValue(record.LocationName);
                    sheet.GetRow(iRow).GetCell(3).SetCellValue(record.Spec);
                    sheet.GetRow(iRow).GetCell(4).SetCellValue(record.PkgQty);
                    sheet.GetRow(iRow).GetCell(5).SetCellValue(record.CountedPkgQty);
                    sheet.GetRow(iRow).GetCell(6).SetCellValue(record.PieceQty);
                    sheet.GetRow(iRow).GetCell(7).SetCellValue(record.CountedPieceQty);
                    sheet.GetRow(iRow).GetCell(8).SetCellValue(record.GoodsQty);
                    sheet.GetRow(iRow).GetCell(9).SetCellValue(record.CountedGoodsQty);
                    iRow++;
                }
                //保存文件
                try
                {
                    FileStream fileOutput = new FileStream(fileDlg.FileName, FileMode.Create);
                    hssfworkbook.Write(fileOutput);
                    fileOutput.Close();
                    InfoSys.SysLogWriter.WriteOptLog("导出盘点单", "ExportExcel", "导出");
                    System.Diagnostics.Process.Start(sOutputDir);

                }
                catch (IOException ex)
                {
                    MessageBox.Show("请先关闭已打开的Excel文件。");
                    return;
                }
                finally
                {
                    Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                }
            }
        }

        private void BtnDBFile_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbCountBillCode.Text))
            {
                MessageBox.Show("请选择盘点单！");
                return;
            }
            BilCountBill ucRecord = new BilCountBill();
            ucRecord.Id = this.tbCountBillCode.Text.Trim();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            ucRecord = client.QueryCountBill(ucRecord);
            if (ucRecord.BillStatus == "0")
            {
                MessageBox.Show("请对盘点单进行 制单确认 ！");
                return;
            }

            SaveFileDialog fileDlg = new SaveFileDialog();
            fileDlg.Filter = "*.db3|*.db3";
            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                string sTemplate = System.Windows.Forms.Application.StartupPath + @"\Template\CountBill.db3";
                if (!File.Exists(sTemplate))
                {
                    MessageBox.Show("模板文件不存在！");
                    return;
                }

                //连接库
                SqliteHelper helper = new SqliteHelper(sTemplate);
                helper.ExecuteNonQuery("delete from CountBill");
                helper.ExecuteNonQuery("delete from BaseInfo");
                //写入数据
                try
                {
                    using (DbConnection conn = DbProviderFactories.GetFactory("System.Data.SQLite").CreateConnection())
                    {
                        conn.ConnectionString = "data source = " + sTemplate;


                        //读取设备信息
                        using (SQLiteCommand insertRngCmd = (SQLiteCommand)conn.CreateCommand())
                        {
                            conn.Open();

                            DbTransaction transaction = conn.BeginTransaction();


                            insertRngCmd.CommandText = @"INSERT INTO BaseInfo 
                                           VALUES  (@CountBillCode, @CountedDate, @OperatorCode, @OperatorName)";
                            insertRngCmd.Parameters.AddWithValue("@CountBillCode", this._currentCountBill.Id);
                            insertRngCmd.Parameters.AddWithValue("@CountedDate", "");
                            insertRngCmd.Parameters.AddWithValue("@OperatorCode", "");
                            insertRngCmd.Parameters.AddWithValue("@OperatorName", "");
                            insertRngCmd.ExecuteNonQuery();

                            insertRngCmd.CommandText = @"INSERT INTO CountBill 
                                            VALUES (@CountBillCode,@StockCode,@GoodsCode,@GoodsName,@GoodsBarCode,@SingleGoodsCode,@GoodsSpec,@IBDate,@LocationCode,@PkgQty,@PieceQty,@CountedPkgQty,@CountedPieceQty,@IsFinished)";
                            foreach (BilCountBillDetail item in this._detailList)
                            {
                                insertRngCmd.Parameters.AddWithValue("@CountBillCode", item.CountBillCode);
                                insertRngCmd.Parameters.AddWithValue("@StockCode", item.StockCode);
                                insertRngCmd.Parameters.AddWithValue("@GoodsCode", item.GoodsCode);
                                insertRngCmd.Parameters.AddWithValue("@GoodsName", item.GoodsName);
                                insertRngCmd.Parameters.AddWithValue("@GoodsBarCode", item.GoodsBarCode);
                                insertRngCmd.Parameters.AddWithValue("@SingleGoodsCode", item.SingleGoodsCode);
                                insertRngCmd.Parameters.AddWithValue("@GoodsSpec", item.Spec);
                                insertRngCmd.Parameters.AddWithValue("@IBDate", item.IBDate);
                                insertRngCmd.Parameters.AddWithValue("@LocationCode", item.LocationCode);
                                insertRngCmd.Parameters.AddWithValue("@PkgQty", item.PkgQty);
                                insertRngCmd.Parameters.AddWithValue("@PieceQty", item.PieceQty);
                                insertRngCmd.Parameters.AddWithValue("@CountedPkgQty", 0);
                                insertRngCmd.Parameters.AddWithValue("@CountedPieceQty", 0);
                                insertRngCmd.Parameters.AddWithValue("@IsFinished", "0");
                                insertRngCmd.ExecuteNonQuery();
                            }
                            transaction.Commit();
                            File.Copy(sTemplate, fileDlg.FileName, true);
                            MessageBox.Show("导出成功！");
                            InfoSys.SysLogWriter.WriteOptLog("导出盘点单交换文件", "ExportDBFile", "导出");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void btnPrintRpt_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbCountBillCode.Text))
            {
                MessageBox.Show("请选择盘点单！");
                return;
            }
            if (this.cbBillStatus.SelectedIndex != 2)
            {
                MessageBox.Show("盘点单状态必须为已盘点才可打印");
                return;
            }
             Report.ReportHelper.PrintCountBill(this._currentCountBill, this._detailList.ToList());
        }

        private void btnCancelBill_Click(object sender, EventArgs e)
        {

            if (this.tbCountBillCode.Text.Trim() == "")
            {
                MessageBox.Show("没有选择任何盘点单，请重新选择！");
                return;
            }

            BilCountBill ucRecord = new BilCountBill();
            ucRecord.Id = this.tbCountBillCode.Text.Trim();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            ucRecord = client.QueryCountBill(ucRecord);
            if (ucRecord.BillStatus == "2")
            {
                MessageBox.Show("该盘点单已完成盘点，无法撤销！");
                return;
            }
            if (MessageBox.Show(string.Format("您是否要撤销盘点单:{0} ?", ucRecord.Id), "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            ucRecord.BillStatus = "3";
            if (client.UpdateCountBill(ucRecord))
            {
                this.cbBillStatus.SelectedIndex = 3;
                MessageBox.Show("提交成功！");
                ClearUI();
            }
            else
            {
                MessageBox.Show("提交失败！");
            }
        }
    }
}
  
